<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13/0013
 * Time: 11:44
 */

namespace app\sports_admin\controller;


use app\sports_admin\model\SuggestionType as SuggestionTypeModel;
use app\sports_admin\vaildate\Suggestion as SuggestionVaildate;
use think\Request;
use app\sports_admin\model\Suggestion as SuggestionModel;
use app\exception\SuggestionException;
class Suggestion extends Base
{
    //信息管理首页
    public function index()
    {
        //类别信息
        $typeInfo = SuggestionTypeModel::select();
        return view('index',compact('typeInfo'));
    }
    /**
     * 获取信息列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $start 检索的开始时间
     * @param $end 检索的结束时间
     * @param $type 信息类别
     * @param $source 信息来源
     */
    public function suggestionList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','start','end']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $start = trim($params['start']);
        $end = trim($params['end']);
        return SuggestionModel::getSuggestionInfo($page,$rows,$sort,$sortOrder,$start,$end);

    }
    /**
     * 添加信息页面
    */
    public function add()
    {
        $type = SuggestionTypeModel::getTypeList();
        return view('add',compact('type'));
    }
    //添加信息
    public function suggestionAdd(Request $request)
    {
        if($request->isAjax()){
            (new SuggestionVaildate())->insert();
            $data = SuggestionModel::suggestionAdd();
            if(!$data){
                throw new SuggestionException([
                    'msg' => '添加失败',
                    'code' => 202
                ]);
            }
            return json(['code'=>200,'msg'=>'添加成功']);
        }
    }
    //删除信息
    public function suggestionDel(Request $request)
    {
        if($request->isAjax()){
            (new SuggestionVaildate())->go_check();
            $data = SuggestionModel::suggestionDel();
            if(!$data){
                throw new SuggestionException([
                    'msg'=>"删除失败",
                    'code'=>400
                ]);
            }
            return json(['code'=>200,'msg'=>"删除成功"]);
        }
    }
}
